package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.modular.system.model.GameTag;
import cn.stylefeng.guns.modular.system.dao.GameTagMapper;
import cn.stylefeng.guns.modular.system.model.GameTagLog;
import cn.stylefeng.guns.modular.system.service.IGameTagLogService;
import cn.stylefeng.guns.modular.system.service.IGameTagService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nineshadow.games.vo.UserTagsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyong
 * @since 2019-11-28
 */
@Service
public class GameTagServiceImpl extends ServiceImpl<GameTagMapper, GameTag> implements IGameTagService {

    @Autowired
    private IGameTagLogService gameTagLogService;

    @Override
    public List<UserTagsVo> getUserTags(Integer userId,Integer GameType) {
        List<UserTagsVo> userTagsVos=new ArrayList<>();
        List<GameTag> gameTags=baseMapper.selectList(new EntityWrapper<>());
        for (GameTag gameTag : gameTags) {
            UserTagsVo userTagsVo=new UserTagsVo();
            userTagsVo.setId(gameTag.getId());
            userTagsVo.setName(gameTag.getTagName());
            int tagCount=gameTagLogService.selectCount(new EntityWrapper<GameTagLog>()
                    .eq("tag_id",gameTag.getId())
                    .eq("target_id",userId).eq("game_type",GameType));
            userTagsVo.setCount(tagCount);
            userTagsVos.add(userTagsVo);
        }
        return userTagsVos;
    }

    @Override
    public boolean isLikeTagByRoom(Integer roomId, Integer userId, Integer tagId) {
        GameTagLog gameTagLog=gameTagLogService.selectOne(new EntityWrapper<GameTagLog>()
                .eq("room_id",roomId).eq("tag_id",tagId)
                .eq("user_id",userId).last("limit 1"));
        if(Objects.nonNull(gameTagLog)){
            return true;
        }
        return false;
    }
}
